package com.yanyeori.framework.redis.config;

import com.yanyeori.framework.redis.constant.RedisConst;
import com.yanyeori.framework.redis.listener.MessageListenerDelegate;
import com.yanyeori.framework.redis.serializer.JacksonSerializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * redis消费者订阅配置
 *
 * @author chenkuan 2020/08/17
 */
@Configuration
@EnableCaching
public class RedisEventListenAutoConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        MessageListenerDelegate delegate = new MessageListenerDelegate();
        delegate.setSerializer(JacksonSerializer.serializer());
        container.addMessageListener(delegate, new PatternTopic(RedisConst.REDIS_EVENT_CHANNEL));
        return container;
    }
}
